﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
namespace Indoor_security
{
    public partial class Form1 : Form
    {
  
        public Form1()
        {
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Open();
                if (serialPort1.IsOpen)
                {
                    MessageBox.Show("程序开启成功!!!");
                }
            }
            catch
            {
                MessageBox.Show("串口打开失败!!!", "提示");
            }
        }

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            
            string receivedData = serialPort1.ReadExisting().TrimEnd('\0');

          
            string[] dataEntries = receivedData.Split(';');
            foreach (string entry in dataEntries)
            {
                string[] tokens = entry.Split('|');
                if (tokens.Length == 2)
                {
                    string dataType = tokens[0]; 
                    string dataValue = tokens[1]; 
                    switch (dataType)
                    {
                        case "a":
                            this.Invoke(new EventHandler(delegate
                            {
                                textBox2.AppendText(dataValue);
                                //textBox2.Text = dataValue;
                            }));
                            break;
                        case "b":
                            this.Invoke(new EventHandler(delegate
                            {
                                textBox3.AppendText(dataValue);
                                //textBox3.Text = dataValue;
                            }));
                            break;
                        case "c":
                            this.Invoke(new EventHandler(delegate
                            {
                                textBox1.AppendText(dataValue);
                                //textBox1.Text = dataValue;
                            }));
                            break;
                        case "d":
                            this.Invoke(new EventHandler(delegate
                            {
                                //textBox4.AppendText(dataValue);
                                textBox4.Text = dataValue;
                            }));
                            break;
                        default:
                            break;
                    }
                }
            }
        }
                           
        private void open_sound_Click(object sender, EventArgs e)
        {
            serialPort1.Write("1");
            MessageBox.Show("开启成功");
        }

        private void close_sound_Click(object sender, EventArgs e)
        {
            serialPort1.Write("0");
            MessageBox.Show("关闭成功!!!");
        }

        private void open_vibrate_Click(object sender, EventArgs e)
        {
            serialPort1.Write("2");
            MessageBox.Show("开启成功!!!");
        }

        private void close_vibrate_Click(object sender, EventArgs e)
        {
            serialPort1.Write("3");
            MessageBox.Show("关闭成功!!!");
        }

        private void open_hongw_Click(object sender, EventArgs e)
        {
            serialPort1.Write("4");
            MessageBox.Show("开启成功!!!");
        }

        private void close_hongw_Click(object sender, EventArgs e)
        {
            serialPort1.Write("5");
            MessageBox.Show("关闭成功!!!");
        }

        private void open_Click(object sender, EventArgs e)
        {
            serialPort1.Write("6");
            MessageBox.Show("打开风扇!!!");
        }

        private void close_Click(object sender, EventArgs e)
        {
            serialPort1.Write("7");
            MessageBox.Show("关闭风扇!!!");
        }
    }
}